package com.kd.controller.jobSeeker;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.kd.bean.jobSeeker.Basic_information;
import com.kd.bean.jobSeeker.Doc_resumes;
import com.kd.service.jobSeeker.IBasic_informationService;
import com.kd.service.jobSeeker.IDoc_resumesService;

@Controller
@RequestMapping("/doc_resumes")
public class Doc_resumesController {

	@Autowired
	private IDoc_resumesService resService;
	
	@Autowired
	private IBasic_informationService basicService;

	
	@RequestMapping("/queryAllByBasic_id")
	@ResponseBody
	public Map<String, Object> queryAllByBasic_id(HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		Basic_information basic_information = (Basic_information) session.getAttribute("basicMessage");
		try {
			List<Doc_resumes> list = resService.queryAllByBasic_id(basic_information.getBasic_id());
			if(list.size()==0) {
				basicService.updateBasic_informationBasic_job_state(1, basic_information.getBasic_id());
			}
			map.put("list", list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}

	@RequestMapping("/insertDDoc_resumes")
	public String insertDDoc_resumes(String doc_title,HttpSession session, Model model,
			@RequestParam("myFile") MultipartFile myFile, HttpServletRequest request) {
		Doc_resumes doc_resumes = new Doc_resumes();
		Basic_information basic_information = (Basic_information) session.getAttribute("basicMessage");
		
		
		// 1、设置上传路径
		String storPath = System.getProperty("user.dir") + "/data/tmp/fileHome";
		// 2、判断该位置存在不存在
		File file = new File(storPath);
		if (!file.getParentFile().exists()) {// 如果父路径不存在
			file.mkdirs();// 则创建目录
		}
		// 3、将文件上传到上传路径
		try {
			File file2 = new File(storPath, myFile.getOriginalFilename());
			myFile.transferTo(file2);
			
			doc_resumes.setBasic_id(basic_information.getBasic_id());
			doc_resumes.setDoc_url(file2.getPath());
			doc_resumes.setDoc_title(myFile.getOriginalFilename());
			
			Boolean flag = resService.insertDDoc_resumes(doc_resumes);
			if(flag) {
				model.addAttribute("msg", "上传成功");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("msg", "上传失败");
		return "/view/resume.jsp";
	}
	
	@RequestMapping("/removeDoc_resumes")
	@ResponseBody
	public Boolean removeDoc_resumes(Integer doc_id) {
		Boolean flag = false;
		try {
			flag = resService.removeDoc_resumes(doc_id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
}
